package bz.ziro.kanbe.controller.mng.listdata;

import java.util.Date;
import java.util.logging.Logger;
import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import bz.ziro.kanbe.model.SiteListData;
import bz.ziro.kanbe.util.KeyFactory;

/**
 * リストデータ追加
 * @author Administrator
 */
public class AddController extends Controller {

    @SuppressWarnings("unused")
    private static final Logger logger = Logger.getLogger(AddController.class.getName());

    @Override
    public Navigation run() {

        //リストデータを生成する
        SiteListData siteListData = new SiteListData();
        String keyBuf = param("ownerKey");
        Key key = KeyFactory.createListKey(Long.valueOf(keyBuf));

        //データを設定する
        siteListData.setListKey(key);
        siteListData.setData((String)requestScope("data"));

        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        //作成日を設定
        siteListData.setCreateDate(new Date());
        siteListData.setCreator(user);

        Key dataKey = KeyFactory.allocateListDataKey(Long.valueOf(keyBuf));
        siteListData.setKey(dataKey);

        //追加する
        Datastore.put(siteListData);
        return null;
    }
}
